<?php

/**
 * author : Tran Minh Toan
 */

//Require model files
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'User.php';
//Require objects files
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'User.php';
//Require forms files
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'User.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'UserEdit.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'UserLogin.php';

class UserController extends Zend_Controller_Action{
    
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private  $modelUser; 
    private $_redirector;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////
    
    
    public function preDispatch() {

    }
    
    public function init(){
        $layoutPath = APPLICATION_PATH . '/templates/default';
        $option = array('layout' => 'index',
        'layoutPath' => $layoutPath);
        
        Zend_Layout::startMvc($option);
        $this->modelUser = new End_Models_User();
        $this->_redirector = $this->_helper->getHelper('Redirector');
    }
    
    ///////////////////////
    // U T I L I T I E S //
    ///////////////////////
   
        
    ///////////////////
    // A C T I O N S //
    ///////////////////
    //INDEX ACTION
    public function indexAction() { 
        $id = $this->getRequest()->getParam('id', '0');
        $user = $this->modelUser->_find($id);
        $this->view->user = $user;
        //die();
    }
    
    //ADD ACTION
    public function addAction(){
        
       //Xóa tất cả các file trong thư mục img, thư mục này lưu những file ảnh captcha tạo ra.
       $files = glob(APPLICATION_PATH . '/../public/media/images/captcha/img/*.*');
            foreach( $files as $file) 
                unlink($file);     
            
       $formUser = new Application_Form_User();
        
        if($this->getRequest()->isGet()){
            $formUser->user_id->setValue($user_id);
        }else{
            $cancel = $this->getRequest()->getPost('cancel', '0');
            if($cancel){
               $this->_redirector->gotoUrl('');
            }
            
            $formData = $this->getRequest()->getPost();
            
            if($formUser->isValid($formData)){
                $user = new User();
                $user->user_address   = $formData['user_address'];
                $user->user_account      = MD5($formData['user_password']);
                $user->user_password     = $formData['user_password'];
                $user->user_fullname     = $formData['user_fullname'];
                $user->user_birthday     = $formData['user_birthday'];
                $user->user_idcard       = $formData['user_idcard'];
                $user->user_role         = 2;
                
                //var_dump($user);die();
                $temp = $this->modelUser->IsExitsAccount($user->user_account);
                if($temp==true){
                    //$this->_redirector->gotoUrl('admin/user/add');
                    echo "Tài khoản " . $user->user_account . " đã tồn tại. Vui lòng chọn tên tài khoản khác.";
                    $formUser->populate($formData);
                }else {
                    //var_dump($user);die();
                    $this->modelUser->_insert($user);
                    $this->_redirector->gotoUrl('');
                }
            }else{
                $formUser->populate($formData);
            }
        }
        $this->view->form = $formUser;
    }
    
    //ADD ACTION
    public function editAction(){
        $formUser = new Application_Form_UserEdit();
        $id = $this->getRequest()->getParam('id', '0');
        $user = $this->modelUser->_find($id);
        
        if(isset ($user)){
            if($this->getRequest()->isPost()){
                $formUser->user_id->setValue($user->user_id);
                $cancel = $this->getRequest()->getPost('cancel', '0');
                if($cancel){
                    $this->_redirector->gotoUrl('user/index/id/'.$user->user_id);
                }
                
                $formData = $this->getRequest()->getPost();
                
                if($formUser->isValid($formData)){
                    $user->user_address   = $formData['user_address'];
                    //$user->user_account      = $formData['user_account'];
                    $user->user_password     = MD5($formData['user_password']);
                    $user->user_fullname     = $formData['user_fullname'];
                    $user->user_birthday     = $formData['user_birthday'];
                    $user->user_idcard       = $formData['user_idcard'];
                    $user->user_role         = $formData['user_role'];
                    $useraccount = $formData['user_account'];
                    if($user->user_account != $useraccount && $this->modelUser->IsExitsAccount($useraccount)==true ){
                        echo "Tài khoản " . $user->user_account . " đã tồn tại. Vui lòng chọn tên tài khoản khác.";
                        $formUser->populate($formData);
                    }else {
                        $user->user_account = $useraccount;
                        $this->modelUser->_update($user);
                        $this->_redirector->gotoUrl('user/index/id/'.$user->user_id);
                    }
                }  else {
                    $formUser->populate($formData);
                }
            }  else {
                $UC = new Utility_Converter();
                $formUser->populate($UC->objectToArray($user));
                //$this->view->form = $formUser;
            }
        }  else {
            $this->_redirector->gotoUrl('');
        }
        $this->view->form = $formUser;
    }
    
    public function deleteAction(){
        $id = $this->getRequest()->getParam('id', '0');
        $warranty = $this->modelUser->_find($id);
        if(isset ($warranty)){
            $this->modelUser->_delete($id);
        }
        $this->_redirector->gotoUrl('');
    }
    
    public function loginAction() {
        ob_start();
        if( isset($_COOKIE['user']) and $_COOKIE['pass'] != ""){
            $this->_redirector->gotoUrl('');
	}else{
            $formLogin = new Application_Form_UserLogin();
            $user = new User();
            if($this->getRequest()->isPost()){
                $cancel = $this->getRequest()->getPost('cancel', '0');
                if($cancel){
                    $this->_redirector->gotoUrl('');
                }
                
                $formData = $this->getRequest()->getPost();
                if($formLogin->isValid($formData)){
                    $user->user_account      = $formData['user_account'];
                    $user->user_password     = MD5($formData['user_password']);
                    
                    $tempUser = $this->modelUser->findByUsername($user->user_account);
                    if($tempUser == NULL){
                        echo "Tài khoản " . $user->user_account . " không tồn tại. Vui lòng nhập đúng tên tài khoản" ;
                    }else if($tempUser->user_password != $user->user_password){
                        echo "Mật khẩu không đúng, vui lòng đăng nhập lại";
                    }else{
                        //var_dump($formData['checkbox_remember']);die();
                        if($formData['checkbox_remember'] == '1'){
                            setcookie('user', $user->user_account, time() + 3600);
                            setcookie('pass', $user->user_password, time() + 3600);
                        }
                        $this->_redirector->gotoUrl('');
                    }
                }
            }
            $this->view->form = $formLogin;
        }
    }
    public function logoutAction() {
	$auth = Zend_Auth::getInstance(); 
	$auth->clearIdentity();
    }    
}